﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NetToolsV2
{
    public partial class Form1 : Form
    {
        private IcmpEcho echoRequest;
        private RttStats rttStats;

        public Form1()
        {
            InitializeComponent();

            echoRequest = new IcmpEcho();
            rttStats = new RttStats();
            echoRequest.OnPingResult += new IcmpEcho.PingResult(echoRequest_OnPingResult);
            echoRequest.OnPingError += new IcmpEcho.PingError(echoRequest_OnPingError);
            echoRequest.OnPingFinished += new IcmpEcho.PingFinished(echoRequest_OnPingFinished);
        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnPing_Click(object sender, EventArgs e)
        {
            int numberOfPings = 4;
            if (!echoRequest.IsRunning)
            {
                rttStats.Clear();
                try
                {
                    numberOfPings = int.Parse(tbPingCount.Text);
                }
                catch (Exception)
                {
                    tbPingCount.Text = numberOfPings.ToString();
                }

                lvResults.Items.Clear();
                echoRequest.NumberOfPings = numberOfPings;
                echoRequest.Ping(txtHostName.Text);
                btnPing.Enabled = false;
                btnTraceRoute.Enabled = false;
            }
        }

        private void btnTraceRoute_Click(object sender, EventArgs e)
        {
            if (!echoRequest.IsRunning)
            {
                lvResults.Items.Clear();
                echoRequest.TraceRoute(txtHostName.Text);
            }
        }

        void echoRequest_OnPingError(string errorMessage)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new IcmpEcho.PingError(echoRequest_OnPingError), new object[] { errorMessage } );
                return;
            }
            lvResults.Items.Add(new ListViewItem(errorMessage));
            lvResults.EnsureVisible(lvResults.Items.Count - 1);
        }

        void echoRequest_OnPingResult(IcmpEchoReply reply)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new IcmpEcho.PingResult(echoRequest_OnPingResult), new object[] { reply });
                return;
            }
            System.Net.IPAddress addr = new System.Net.IPAddress(reply.Address);
            string IPAddress = addr.ToString();
            ListViewItem item = new ListViewItem(IPAddress);
            item.SubItems.Add(reply.RoundTripTime.ToString());
            lvResults.Items.Add(item);
            rttStats.UpdateStats(reply.RoundTripTime);
            lvResults.EnsureVisible(lvResults.Items.Count - 1);
        }

        void echoRequest_OnPingFinished()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new IcmpEcho.PingFinished(echoRequest_OnPingFinished));
                return;
            }
            string results = "RTT Statistics: ";
            lvResults.Items.Add(new ListViewItem(results));
            results = string.Format("Min:{0}, Max:{1}, Average:{2}", rttStats.MinRTT, rttStats.MaxRTT, rttStats.AverageRTT);
            lvResults.Items.Add(new ListViewItem(results));
            TimeSpan duration = DateTime.Now - rttStats.StartOfCollection;
            results = string.Format("{0} pings in {1} milliseconds", rttStats.PaketCount, duration.TotalMilliseconds);
            lvResults.Items.Add(new ListViewItem(results));
            lvResults.EnsureVisible(lvResults.Items.Count - 1);
            btnPing.Enabled = true;
            btnTraceRoute.Enabled = true;
        }
    }
}